package cn.swu.lzr.event;

import cn.swu.lzr.JDBC.DBTools;
import cn.swu.lzr.common.Utils;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.http.*;
import org.apache.commons.fileupload2.core.DiskFileItemFactory;
import org.apache.commons.fileupload2.core.FileItem;
import org.apache.commons.fileupload2.jakarta.JakartaServletFileUpload;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
@MultipartConfig
public class InsertEventServlet extends HttpServlet {

    private static final int MAX_FILE_SIZE      = 1024 * 1024 * 40; // 40MB
    private static final int MAX_REQUEST_SIZE   = 1024 * 1024 * 50; // 50MB
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

        boolean multipartContent = JakartaServletFileUpload.isMultipartContent(request);
        if (!multipartContent){
            throw new RuntimeException("非要求上传类型");
        }


        //上传解析对象
        DiskFileItemFactory.Builder builder = new DiskFileItemFactory.Builder();
        builder.setCharset("UTF-8");
       DiskFileItemFactory factory = builder.get();

        JakartaServletFileUpload upload = new JakartaServletFileUpload(factory);
        upload.setSizeMax(MAX_REQUEST_SIZE);
        upload.setSizeMax(MAX_FILE_SIZE);
        upload.setHeaderCharset(Charset.forName("UTF-8"));

        //上传路径
        String UploadPath = request.getServletContext().getRealPath("./")
        + File.separator +"image"+File.separator+"upload";

        File uploadDir = new File(UploadPath);
       if (!uploadDir.exists())
        {
            uploadDir.mkdirs();
        }

        try{
           List<FileItem> formItems = upload.parseRequest(request);

            if (formItems != null && formItems.size() > 0)
            {
                Event event = new Event();
                for (FileItem item :formItems) {



                    if (!item.isFormField()) {

                        if (item.getName() != null && !item.getName().trim().equals("")) {
                            File original = new File(item.getName());
                            String fileName = UUID.randomUUID().toString() + Utils.getFileSuffix(original);
                            Path filePath = Paths.get(UploadPath, fileName);
                            item.write(filePath);
                            event.setPicture(fileName);
                        }
                    } else {

                        String name = item.getFieldName();
                        if (name.equals("date")) {
                            event.setDate(item.getString());
                        } else if (name.equals("event")) {
                            event.setEvent(item.getString());
                        }
                    }
                }
                HttpSession session = request.getSession();
                Integer id = (Integer) session.getAttribute("Updateid");
                DBTools dbTools = new DBTools();

                if (id == null)
                {
                    dbTools.insertEvent(event);
                }else {
                    event.setId(id);
                    dbTools.UpdateEventById(id,event);
                    session.setAttribute("Updateid",null);
                }

            }

       } catch (Exception e) {
           e.printStackTrace();
       }
        response.sendRedirect("/java-design/index.html");
   }
}
